package com.fb.cvstool;

import java.io.File;
import java.io.FileInputStream;
import java.io.IOException;
import java.io.InputStream;
import java.net.URISyntaxException;
import java.util.ArrayList;
import java.util.Iterator;
import java.util.List;
import java.util.Properties;

import org.netbeans.lib.cvsclient.command.CommandAbortedException;
import org.netbeans.lib.cvsclient.command.CommandException;
import org.netbeans.lib.cvsclient.connection.AuthenticationException;

public class CVSDemo {

	public static void main(String[] args) throws AuthenticationException,
			IOException, CommandException, URISyntaxException {

		/*
		 * <p>set the connection string of the cvs server</p> connect string
		 * provide protocol,hostname,user,password,repository
		 * e.g:":pserver:ibmuser:icmadmin@localhost:d:/root"
		 * method:[[user][:password]@]hostname[:[port]]/path/to/repository
		 * 
		 * e.g.
		 * :pserver;username=anonymous;hostname=localhost:/path/to/repository
		 */
		System.out.println("start here!");
		String path = System.getProperty("user.dir");
		InputStream fis = new FileInputStream(new File(path + "\\"
				+ "config.properties"));
		Properties p = new Properties();
		p.load(fis);
		String cvsroot = p.getProperty("cvsroot");
		System.out.println(cvsroot);

		// open the connection

		// set the local directory
		String localpath = p.getProperty("localpath");
		// cvsclient.setLocalPath(localpath);
		// System.out.println(localpath);

		/* Demo of excute command as follow */
		// add & commit
		/*
		 * String filepath = p.getProperty("needCommitFilesFile");
		 * 
		 * FileInputStream ffis = new FileInputStream(filepath); DataInputStream
		 * dis = new DataInputStream(ffis); BufferedReader d = new
		 * BufferedReader(new InputStreamReader(dis));
		 * 
		 * List<String> addFielsList = new ArrayList<String>(); List<String>
		 * updateFilesList = new ArrayList<String>(); List<String>
		 * deleteFilesList = new ArrayList<String>();
		 * 
		 * while (d.read() != -1) { String str = d.readLine(); // process file
		 * if (str.startsWith("N")) { addFielsList.add(splitFileName(str)); }
		 * else if (str.startsWith("U")) {
		 * updateFilesList.add(splitFileName(str)); } else if
		 * (str.startsWith("D")) { deleteFilesList.add(splitFileName(str)); } }
		 * 
		 * String[] addFiles = new String[addFielsList.size()]; String[]
		 * updateFiles = new String[updateFilesList.size()]; String[]
		 * deleteFiles = new String[deleteFilesList.size()]; // new files
		 * cvsclient.excute(cvsclient.add(addFiles));
		 * cvsclient.excute(cvsclient.commit(addFiles)); // change files
		 * cvsclient.excute(cvsclient.commit(updateFiles));
		 * 
		 * //delete files for (int i = 0; i < deleteFiles.length; i++) { File
		 * file = new File(deleteFiles[0]); file.delete(); }
		 * cvsclient.excute(cvsclient.remove(deleteFiles));
		 * cvsclient.excute(cvsclient.commit(deleteFiles));
		 */

		// cvsclient.historysincedate("");

		// check out
		// cvsclient.excute(cvsclient.checkout("testwebapp"));
		// cvsclient.excute(cvsclient.checkouttoOutput("testwebapp"));

		// update
		// cvsclient.excute(cvsclient.update(new
		// String[]{p.getProperty("localpath")}));

		// diff
		// cvsclient.excute(cvsclient.diffbyreveision(new
		// String[]{"d:/client/java/abc/abc.java"}, "1.1", "1.2"));
		// cvsclient.excute(cvsclient.diffbydate(new
		// String[]{"D:/client/java/abc/abc.java"}, "20070206", "20070207"));

		// history
		// cvsclient.excute(cvsclient.historysincedate("20070201"));
		// cvsclient.excute(cvsclient.historysincerRevision("1.1"));

		ArrayList<String> fileList = new ArrayList<String>();
		fileList.add("CVS_repository/root/WebRoot/WEB-INF/web.xml");
		fileList.add("CVS_repository/root/WebRoot/WEB-INF/struts-config.xml");
		fileList.add("CVS_repository/root/WebRoot/WEB-INF/testdir/empty5.txt");

		checkoutUpdatedFilesIntoTargetDir("D:\\cvs_checkout\\bak", cvsroot,
				fileList, null);

		System.out.println("End!");
	}

	public static void checkoutUpdatedFilesIntoTargetDir(String targetDir,
			String cvsroot, List<String> updatedFiles, String branch) {

		CVSClient cvsclient = new CVSClient(cvsroot);
		try {
			Iterator<String> itr = updatedFiles.iterator();
			while (itr.hasNext()) {
				cvsclient.openConnection();
				cvsclient.setLocalPath(targetDir);
				String filepath = (String) itr.next();
				cvsclient.setLocalPath(targetDir);
				cvsclient.excute(cvsclient.checkout(filepath,branch));
				cvsclient.closeConnection();
			}
		} catch (CommandAbortedException e) {
			e.printStackTrace();
			System.exit(0);
		} catch (AuthenticationException e) {
			e.printStackTrace();
			System.exit(0);
		} catch (IOException e) {
			e.printStackTrace();
			System.exit(0);
		} catch (CommandException e) {
			e.printStackTrace();
		}

	}

	public static String splitFileName(String str) {
		System.out.println("--wait to coding here!");
		return str;
	}
}